local skel = fk.CreateSkill {
  name = "lb_zero__mengyao",
  dynamic_desc = function(self, player, lang)
    return "lb_zero__mengyao_inder:" .. Fk:translate("#lb_zero__mengyao_" .. player:getMark("lb_zero__mengyao"))
  end,
}

Fk:loadTranslationTable {
  ["lb_zero__mengyao"] = "梦谣",
  --[":lb_zero__mengyao"] = "每轮开始时，或你于每轮首次造成伤害后，你可从三个随机的<a href='#lb_zero__mengyao_skills'><font color='red'>“普通以骸”</font></a>技能中获得一个，若你已因此获得三个技能，改为替换一个技能。",
  --[":lb_zero__mengyao_inder"] = "每轮开始时，或你于每轮首次造成伤害后，你可从三个随机的<a href='#lb_zero__mengyao_skills'><font color='red'>“{1}以骸”</font></a>技能中获得一个，若你已因此获得三个技能，改为替换一个技能。",

  [":lb_zero__mengyao"] = "游戏开始时，或你于每轮首次造成或受到伤害后，你可从两个随机的<a href='#lb_zero__mengyao_skills'><font color='red'>“普通以骸”</font></a>技能中获得一个，若你已因此获得三个技能，改为替换一个技能。",
  [":lb_zero__mengyao_inder"] = "游戏开始时，或你于每轮首次造成或受到伤害后，你可从两个随机的<a href='#lb_zero__mengyao_skills'><font color='red'>“{1}以骸”</font></a>技能中获得一个，若你已因此获得三个技能，改为替换一个技能。",

  ["#lb_zero__mengyao"] = "梦谣：你可从两个随机的“%arg以骸”技能中获得一个，若你已因此获得三个技能，改为替换一个技能",
  ["#lb_zero__mengyao-ask"] = "梦谣：请将 〖%arg〗 替换一个技能",

  ["#lb_zero__mengyao_1"] = "普通",
  ["#lb_zero__mengyao_2"] = "精英",
  ["#lb_zero__mengyao_3"] = "高危",

  ["@[lb_zero__mengyao]lb_zero__mengyao"] = "梦谣",

  ["#lb_zero__mengyao_skills"] =
      "普通以骸：" ..
      "<a href=':lb_zero__mengyao_leizhu'><font color='red'>〖雷蛛〗</font></a> <a href=':lb_zero__mengyao_tierfeng'><font color='red'>〖提尔锋〗</font></a> <a href=':lb_zero__mengyao_apeika'><font color='red'>〖阿佩卡〗</font></a> <a href=':lb_zero__mengyao_satier'><font color='red'>〖萨提尔〗</font></a> <a href=':lb_zero__mengyao_mandela'><font color='red'>〖曼德拉〗</font></a> <a href=':lb_zero__mengyao_eling'><font color='red'>〖恶灵〗</font></a> <a href=':lb_zero__mengyao_youhun'><font color='red'>〖游魂〗</font></a> <br>" ..
      "精英以骸：" ..
      "<a href=':lb_zero__mengyao_hati'><font color='red'>〖哈提〗</font></a> <a href=':lb_zero__mengyao_tanatuosi'><font color='red'>〖塔纳托斯〗</font></a> <a href=':lb_zero__mengyao_dulahan'><font color='red'>〖杜拉罕〗</font></a> <a href=':lb_zero__mengyao_bannileike'><font color='red'>〖班尼雷克〗</font></a> <a href=':lb_zero__mengyao_yurenxie'><font color='red'>〖渔人蟹〗</font></a> <br>" ..
      "高危以骸：" ..
      "<a href=':lb_zero__mengyao_tepeishi'><font color='red'>〖特佩什〗</font></a> <a href=':lb_zero__mengyao_high_zhuangjiahati'><font color='red'>〖恶名·装甲哈提〗</font></a> <a href=':lb_zero__mengyao_high_dulahan'><font color='red'>〖恶名·杜拉罕〗</font></a> <a href=':lb_zero__mengyao_high_tanatuosi'><font color='red'>〖恶名·塔纳托斯〗</font></a>",
  ["$lb_zero__mengyao1"] = "响应我的召唤！",
  ["$lb_zero__mengyao2"] = "从无数故事、歌谣和梦境中来！",
}

---@param player Player
local getMengYao = function(player)
  local all_skills = {
    { "lb_zero__mengyao_leizhu",   "lb_zero__mengyao_tierfeng",           "lb_zero__mengyao_apeika",       "lb_zero__mengyao_satier",        "lb_zero__mengyao_mandela", "lb_zero__mengyao_eling", "lb_zero__mengyao_youhun" },
    { "lb_zero__mengyao_hati",     "lb_zero__mengyao_tanatuosi",          "lb_zero__mengyao_dulahan",      "lb_zero__mengyao_bannileike",    "lb_zero__mengyao_yurenxie" },
    { "lb_zero__mengyao_tepeishi", "lb_zero__mengyao_high_zhuangjiahati", "lb_zero__mengyao_high_dulahan", "lb_zero__mengyao_high_tanatuosi" }
  }
  local has_skills = {} -- 已有技能
  for i, skill_list in ipairs(all_skills) do
    has_skills[i] = {}
    for _, skill in ipairs(skill_list) do
      if player:hasSkill(skill, true) then
        table.insert(has_skills[i], skill)
      end
    end
  end
  return has_skills
end

Fk:addQmlMark {
  name = "lb_zero__mengyao",
  how_to_show = function(name, value, player)
    local list = getMengYao(player)
    if #list == 0 then
      return "#hidden"
    else
      local mark = {}
      for i, skills in ipairs(list) do
        if #skills > 0 then
          table.insert(mark, { i, #skills })
        end
      end
      return table.concat(table.map(mark, function(val)
        local m = Fk:translate("#lb_zero__mengyao_" .. val[1]) .. tostring(val[2])
        if player:getMark(skel.name) == val[1] then
          m = "<font color='red'>".. m .."</font>"
        end
        return m
      end), "/")
    end
  end,
  qml_data = function(name, value, player)
    local list = getMengYao(player)
    local data = {}
    for i, skills in ipairs(list) do
      if #skills > 0 then
        local skill_list = {Fk:translate("#lb_zero__mengyao_" .. i)}
        for _, val in ipairs(skills) do
          local val_list = val
          if type(val) == "string" then
            val_list = { val, 0 } -- 默认点亮
          end
          table.insert(skill_list, val_list)
        end
        table.insert(data, skill_list)
      end
    end
    return data
  end,
  qml_path = "packages/lb_kill_zero/qml/MengYaoMark"
}

local mengyao_spec = {
  ---@param player ServerPlayer
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player,
      { skill_name = skel.name, prompt = "#lb_zero__mengyao:::" .. "#lb_zero__mengyao_" .. player:getMark(skel.name) })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local has_skills = {} -- 已有技能
    for _, skill_list in ipairs(getMengYao(player)) do
      for _, skill in ipairs(skill_list) do
        table.insert(has_skills, skill)
      end
    end
    local skills = player:getTableMark("lb_zero__mengyao_skills")
    local player_skills = {} -- 可选技能
    local skill_list = table.clone(skills[player:getMark(skel.name)])
    for _, skill in ipairs(skill_list) do
      if not (table.contains(has_skills, skill) or table.contains(player_skills, skill)) then
        table.insert(player_skills, skill)
      end
    end
    if #player_skills == 0 then return end
    if #player_skills > 1 then
      player_skills = table.random(player_skills, 2)
    end
    local choice_skill = room:askToChoice(player, {
      choices = player_skills,
      skill_name = skel.name,
      prompt = "#lb_zero__mengyao:::" .. "#lb_zero__mengyao_" .. player:getMark(skel.name),
      detailed = true,
    })
    skill_list = { choice_skill }
    if #has_skills > 2 then
      local choice = room:askToChoice(player, {
        choices = has_skills,
        skill_name = skel.name,
        prompt = "#lb_zero__mengyao-ask:::" .. choice_skill,
        detailed = true,
      })
      table.insert(skill_list, 1, "-" .. choice)
    end
    room:handleAddLoseSkills(player, skill_list)
  end,
}

skel:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_cost = mengyao_spec.on_cost,
  on_use = mengyao_spec.on_use,
})

skel:addEffect(fk.Damage, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player then
      local use_events = player.room.logic:getEventsOfScope(GameEvent.Damage, 1, function(e)
        local damage = e.data
        return damage.from == player and not data.prevented
      end, Player.HistoryRound)
      return #use_events == 1 and use_events[1].data == data
      --return true
    end
  end,
  on_cost = mengyao_spec.on_cost,
  on_use = mengyao_spec.on_use,
})

skel:addEffect(fk.Damaged, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player then
      local use_events = player.room.logic:getEventsOfScope(GameEvent.Damage, 1, function(e)
        local damage = e.data
        return damage.to == player and not data.prevented
      end, Player.HistoryRound)
      return #use_events == 1 and use_events[1].data == data
      --return true
    end
  end,
  on_cost = mengyao_spec.on_cost,
  on_use = mengyao_spec.on_use,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:setPlayerMark(player, skel.name, 1)
  player.room:setPlayerMark(player, "lb_zero__mengyao_skills",
    {
      { "lb_zero__mengyao_leizhu",   "lb_zero__mengyao_tierfeng",           "lb_zero__mengyao_apeika",       "lb_zero__mengyao_satier",        "lb_zero__mengyao_mandela", "lb_zero__mengyao_eling", "lb_zero__mengyao_youhun" },
      { "lb_zero__mengyao_hati",     "lb_zero__mengyao_tanatuosi",          "lb_zero__mengyao_dulahan",      "lb_zero__mengyao_bannileike",    "lb_zero__mengyao_yurenxie" },
      { "lb_zero__mengyao_tepeishi", "lb_zero__mengyao_high_zhuangjiahati", "lb_zero__mengyao_high_dulahan", "lb_zero__mengyao_high_tanatuosi" }
    })
  player.room:setPlayerMark(player, "@[lb_zero__mengyao]lb_zero__mengyao", 1)
  --player.room:handleAddLoseSkills(player, "lb_zero__mengyao_leizhu|lb_zero__mengyao_tierfeng|lb_zero__mengyao_apeika")
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, skel.name, 0)
  player.room:setPlayerMark(player, "@[lb_zero__mengyao]lb_zero__mengyao", 0)
end)

return skel
